package com.atguigu.ssm.demo.service.impl;

import com.atguigu.ssm.demo.entity.GourdBaby;
import com.atguigu.ssm.demo.entity.GourdBabyExample;
import com.atguigu.ssm.demo.mapper.GourdBabyMapper;
import com.atguigu.ssm.demo.service.api.GbSerive;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author : zzf
 * @version : 1.0
 * @date : 2021/1/13
 */

@Service
public class GbServiceImpl implements GbSerive {
    @Autowired
    private GourdBabyMapper gourdBabyMapper;

    @Override
    @Transactional(
            readOnly = false,
            propagation = Propagation.REQUIRES_NEW,
            rollbackFor = Exception.class
    )
    public void updateGb(GourdBaby gourdBaby) {
        gourdBabyMapper.updateByPrimaryKey(gourdBaby);
    }

    @Override
    public List<GourdBaby> getAll(Integer page) {
        int pageNum = page;
        int pageSize = 3;

        // 调用静态方法，设定pageNum和pageSize，这样PageHelper插件就可以执行分页了
        PageHelper.startPage(pageNum, pageSize);
        GourdBabyExample example = new GourdBabyExample();
        return gourdBabyMapper.selectByExample(example);
    }

    @Override
    public void remove(Integer id) {
        gourdBabyMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<String> getColorList() {
        return gourdBabyMapper.getColorList();
    }

    @Override
    public void saveGb(GourdBaby gourdBaby) {
        gourdBabyMapper.insert(gourdBaby);
    }

    @Override
    public GourdBaby getGb(Integer id) {
        return gourdBabyMapper.selectByPrimaryKey(id);
    }
}
